package com.example.service;

import com.example.entity.Major;
import com.example.entity.Teacher;
import com.example.entity.User;
import com.example.exception.CustomException;
import com.example.mapper.ClazzMapper;
import com.example.mapper.TeacherMapper;
import com.example.mapper.UserMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherService {
    @Resource
    private TeacherMapper teacherMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private ClazzMapper clazzMapper;

    public PageInfo<Teacher> selectPage(Teacher teacher, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Teacher> list = teacherMapper.selectAll(teacher);
        return PageInfo.of(list);
    }

    public List<Teacher> selectByMajorId(Integer majorId) {
        return teacherMapper.selectByMajorId(majorId);
    }


    public void addTeacher(Teacher teacher) {
        // 1. 插入用户表（User）
        if (teacherMapper.countByNumber(teacher.getTeacherNumber()) > 0){
            throw new CustomException("400","工号已存在");
        }
        User user = new User();
        user.setUsername(teacher.getTeacherName()+teacher.getTeacherNumber());
        user.setUserRole("teacher");                     // 设置角色为老师
        user.setPassword("123456");                  // 默认密码
        userMapper.insert(user);                      // 插入后，user.getUserId()会返回自增ID
        // 2. 插入老师表（Teacher）
        teacher.setTeacherId(user.getUserId());       // 老师ID = 用户ID
        teacherMapper.insert(teacher);
    }
    public void updateTeacher(Teacher teacher) {
        Teacher exstingteacher = teacherMapper.selevtById(teacher.getTeacherId());
        if (exstingteacher.getTeacherNumber().equals(teacher.getTeacherNumber())){
            if (exstingteacher.getTeacherName().equals(teacher.getTeacherName()) && exstingteacher.getCollegeId().equals(teacher.getCollegeId())){
                return;
            }
        } else {
            if (teacherMapper.countByNumber(teacher.getTeacherNumber()) > 0){
                throw new CustomException("400","工号已存在");
            }
        }
        // 同时修改用户的昵称
        User user = new User();
        user.setUserId(teacher.getTeacherId());
        user.setUsername(teacher.getTeacherName()+teacher.getTeacherNumber());
        userMapper.update(user);

        teacherMapper.updateTeacher(teacher);
    }

    public void deleteTeacher(Integer teacherId) {
        if (clazzMapper.countByTeacherId(teacherId) > 0) {
            throw new CustomException("400","存在关联班级");
        }
        teacherMapper.deleteTeacher(teacherId);
        userMapper.delete(teacherId);
    }


    public void deleteBatch(List<Integer> ids) {
        for (Integer teacherId : ids) {
            if (clazzMapper.countByTeacherId(teacherId) > 0) {
                throw new CustomException("400","存在关联班级");
            }
        }
        for (Integer teacherId : ids) {
            teacherMapper.deleteTeacher(teacherId);
            userMapper.delete(teacherId);
        }
    }
}
